package util;

import org.json.JSONArray;
import org.json.JSONObject;
import java.io.File;
import java.util.HashMap;


/**
 * Created by yijy on 2017/6/21.
 * face++身份证识别
 */
public class FacePlusIdCard {

    public static void main(String[] args) throws Exception{
        File file = new File("D:/搜狗高速下载/身份证正面.png");
//        File file = new File("D:/搜狗高速下载/身份证反面.png");
        FaceTest faceTest = new FaceTest();
        byte[] buff = faceTest.getBytesFromFile(file);
        String url = "https://api-cn.faceplusplus.com/cardpp/v1/ocridcard";
        HashMap<String, String> map = new HashMap<String, String>(3);
        HashMap<String, byte[]> byteMap = new HashMap<String, byte[]>();
        map.put("api_key", "-y7kHT9zh8ZhGTioOAiGDh_XWb9RT7Dx");
        map.put("api_secret", "qrWajbljdYsH-M8qgVlzB62DDg1f6mtC");
        map.put("legality", "1");

        byteMap.put("image_file", buff);
        try{
            String bacd = faceTest.post(url, map, byteMap);
            JSONObject resultObj = new JSONObject(bacd);
            JSONArray outputArray = resultObj.getJSONArray("cards");
            System.out.println(outputArray);
            String out = outputArray.get(0).toString();
            System.out.println(out);
            JSONObject outObject = new JSONObject(outputArray.get(0).toString());
            String configstr = outObject.getString("side");
            if(!configstr.isEmpty() && "front".equals(configstr)){
                String addr = outObject.getString("address"); // 获取地址
                String name = outObject.getString("name"); // 获取名字
                String num = outObject.getString("id_card_number"); // 获取身份证号
                String sex = outObject.getString("gender"); // 获取性别
                String birth = outObject.getString("birthday"); // 获取身份证号
                String nationality = outObject.getString("race"); // 获取民族
                System.out.printf("addr : %s \n  name : %s \n configstr : %s\n  num : %s\n sex : %s\n birth : %s\n nationality : %s\n",
                        addr, name,configstr,num, sex, birth,nationality);
            }

            if(!configstr.isEmpty() && configstr.equals("back")){
                String valid_date = outObject.getString("valid_date"); //获取有效期起始时间
                String issue = outObject.getString("issued_by"); // 获取签发机关
                System.out.printf(" configstr : %s \n valid_date : %s\n issue : %s\n ", configstr, valid_date,issue);
            }
        }catch (Exception e) {
            e.printStackTrace();
        }
    }

}
